import cache from '../enum/cache'

export function wxToPromise(method, option = {}) {
	return new Promise((resolve, reject) => {
		option.success = resolve
		option.fail = (err) => {
			reject(err)
		}
		wx[method](option)
	})
}

const setTabBarBadge = async function (unreadCount) {
	try {
		if (unreadCount > 0) {
			await wx.setTabBarBadge({
				index: 2,
				text: unreadCount.toString(),
			})
		} else {
			await wx.removeTabBarBadge({
				index: 2,
			})
		}
		wx.setStorageSync(cache.UNREAD_COUNT, 0)
	} catch (e) {
		wx.setStorageSync(cache.UNREAD_COUNT, unreadCount)
	}
}
export { setTabBarBadge }
